/****************************************************************************
	
	Date: Feb, 2018
	Updated: July 2022
	
	Program: Prepare the panel for the motivation regressions
	
	Input:
	- GDP data saved in $datapathother
	- Year by year balance sheet and customs data from previous steps
	
	Output
	- "$outputpath/VA_GDP_Trade_wide_${sample}"
	
****************************************************************************/


*** +++ Collect GDP Data +++ ***

cd "$datapathother"
clear
insheet using NominalGDPUSD_WorldBank.csv, names delimiter(";")
forvalues i=5(1)62{
	local j=`i'+1955
	rename v`i' Nominal_GDP_USD`j'
}
reshape long Nominal_GDP_USD,i(countrycode) j(year)
drop countryname indicatorname indicatorcode
sort countrycode year
encode countrycode, gen(COUNTRY)
tsset COUNTRY year
gen ln_Nominal_GDP_USD = ln(Nominal_GDP_USD)
gen gr_Nominal_GDP_USD = D.ln_Nominal_GDP_USD
drop COUNTRY
drop if year==2017
sort countrycode year
save temp_gdp, replace

use WB_Macro_Data, clear
bys year: egen World_Total = sum(rgdp_usd)
bys year: egen World_Total_old = sum(rgdp_usd_old)
replace countrycode="ROW" if countrycode=="AFG"
replace country="Rest of World" if countrycode=="ROW"
replace rgdp_lcu = 0 if countrycode=="ROW"
sort countrycode year
merge 1:1 countrycode year using temp_gdp
drop _merge
gen keeper=0
foreach i in $wiotcountry_FRA {
	replace keeper = 1 if countrycode=="`i'"
}
keep if keeper==1
bys year: egen Group_Total = sum(rgdp_usd)
bys year: egen Group_Total_old = sum(rgdp_usd_old)
replace rgdp_lcu = World_Total-Group_Total if countrycode=="ROW"
replace rgdp_usd_old = World_Total_old-Group_Total_old if countrycode=="ROW"
rename rgdp_lcu Real_GDP
rename rgdp_usd Real_GDP_USD
rename rgdp_usd_old Real_GDP_USD_old
encode country, gen(COUNTRY)
tsset COUNTRY year
gen ln_Real_GDP = ln(Real_GDP)
gen gr_Real_GDP = D.ln_Real_GDP
gen ln_Real_GDP_USD = ln(Real_GDP_USD)
gen gr_Real_GDP_USD = D.ln_Real_GDP_USD
gen ln_Real_GDP_USD_old = ln(Real_GDP_USD_old)
gen gr_Real_GDP_USD_old = D.ln_Real_GDP_USD_old
gen lag_Nominal_GDP_USD = L.Nominal_GDP_USD
drop World_Total World_Total_old Group_Total Group_Total_old keeper country ln_Real_GDP ln_Real_GDP_USD ln_Real_GDP_USD_old COUNTRY usd_exchange ln_Nominal_GDP_USD
reshape wide Nominal_GDP_USD Real_GDP Real_GDP_USD Real_GDP_USD_old gr_Nominal_GDP_USD gr_Real_GDP gr_Real_GDP_USD gr_Real_GDP_USD_old gdp_deflator lag_Nominal_GDP_USD, i(year) j(countrycode) string
rename gdp_deflatorFRA gdp_infl
drop gdp_deflator*
replace gdp_infl = gdp_infl/100
save GDPs.dta, replace



use WB_Macro_Data, clear
bys year: egen World_Total = sum(rgdp_usd)
bys year: egen World_Total_old = sum(rgdp_usd_old)
replace countrycode="ROW" if countrycode=="AFG"
replace country="Rest of World" if countrycode=="ROW"
replace rgdp_lcu = 0 if countrycode=="ROW"
sort countrycode year
merge 1:1 countrycode year using temp_gdp
drop _merge
keep if countrycode=="FRA"
bys year: egen Group_Total = sum(rgdp_usd)
bys year: egen Group_Total_old = sum(rgdp_usd_old)
rename rgdp_lcu Real_GDP
rename rgdp_usd Real_GDP_USD
rename rgdp_usd_old Real_GDP_USD_old
encode country, gen(COUNTRY)
tsset COUNTRY year
gen ln_Real_GDP = ln(Real_GDP)
gen gr_Real_GDP = D.ln_Real_GDP
gen ln_Real_GDP_USD = ln(Real_GDP_USD)
gen gr_Real_GDP_USD = D.ln_Real_GDP_USD
gen ln_Real_GDP_USD_old = ln(Real_GDP_USD_old)
gen gr_Real_GDP_USD_old = D.ln_Real_GDP_USD_old
gen lag_Nominal_GDP_USD = L.Nominal_GDP_USD
keep year gr_Real_GDP
rename gr_Real_GDP _gr_Real_GDPFRA
sort year
save GDP_FRA.dta, replace

use GDPs.dta, clear
g gr_Real_GDPWorld = 0
g lag=0
foreach i in AUS AUT BEL BGR BRA CAN CHN CYP CZE DEU DNK ESP EST FIN GBR GRC HUN IDN IND IRL ITA JPN KOR LTU LVA MEX MLT NLD POL PRT ROU RUS  SVK SVN SWE TUR USA {
	replace gr_Real_GDPWorld = gr_Real_GDPWorld+gr_Real_GDP`i'*lag_Nominal_GDP_USD`i'
	replace lag=lag+lag_Nominal_GDP_USD`i'
}
replace gr_Real_GDPWorld=gr_Real_GDPWorld/lag
keep year gr_Real_GDPWorld
sort year
save gr_GDPWorld.dta, replace

*** +++ Collect Firm Level Data +++ ***

cd "$outputpath"
forvalues y=1996(1)2008{
	use "$outputpath/bilateralimports_wide_${sample}_`y'", clear

	foreach i in $wiotcountry {
		g import`i'=0
		foreach j in $wiotsector {
			cap g import`i'`j'=0
			replace import`i'`j'=0 if import`i'`j'==.
			replace import`i'=import`i'+ import`i'`j'
			drop import`i'`j'
		}
	}
	keep siren year import*
	sort siren year
	save temp, replace

	use "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_`y'", clear
	keep siren year wiot12_jp vabcf gr_* cafranc pre_gamma* 
	merge m:1 year using "$datapathother/GDPs"
	drop if _merge==2
	drop _merge
	merge m:1 year using "$datapathother/GDP_FRA"
	drop if _merge==2
	drop _merge
	merge m:1 year using "$datapathother/gr_GDPWorld"
	drop if _merge==2

	
	foreach var in vabcf saltrai firm_inputs firm_import{
		gen gr_`var'_real=0
		replace gr_`var'_real=gr_`var'-gdp_inf
	}

	bys year: egen total_vabcf=sum(vabcf)
	gen vabcf_share = vabcf/total_vabcf
	drop total_vabcf _merge gdp_inf

	sort siren year
	g import_intensity = 0
	foreach i in $wiotcountry {
		g import_intensity_`i'=0
	}
	foreach i in $wiotcountry {
		foreach s in $wiotsector_ALLXJP {
			replace import_intensity = import_intensity + pre_gamma_`i'`s'
			replace import_intensity_`i' = import_intensity_`i' + pre_gamma_`i'`s'
		}
	}
	drop pre_gamma*
	
	merge 1:1 siren year using temp
	drop if _merge == 2
	drop _merge
	foreach i in $wiotcountry {
		 cap drop import`i'71t74
		 cap drop import`i'O
		
	}
	compress
	merge 1:1 siren year using "$outputpath/bilateralexports_wide_${sample}_`y'"
	drop if _merge == 2
	drop _merge
	g _=`y'
	if _>1996{
		append using "$outputpath/VA_GDP_Trade_wide_${sample}"
	}
	drop _
	save "$outputpath/VA_GDP_Trade_wide_${sample}", replace
	*erase "$outputpath/Firm_Level_pi_alpha_gamma_${method}_`y'.dta"
	*erase "$datapath/bilateralimports_wide_`y'.dta"
}

use "$outputpath/VA_GDP_Trade_wide_${sample}", clear
gsort siren year
gegen SIREN=group(siren)
tsset SIREN year
replace gr_vabcf=F.gr_vabcf
replace gr_saltrai=F.gr_saltrai
replace gr_firm_inputs=F.gr_firm_inputs
replace gr_firm_import=F.gr_firm_import
replace gr_vabcf_real=F.gr_vabcf_real
replace gr_saltrai_real=F.gr_saltrai_real
replace gr_firm_inputs_real=F.gr_firm_inputs_real
replace gr_firm_import_real=F.gr_firm_import_real


drop SIREN

foreach i in $wiotcountry {
	gen Dummy_From_`i'=0
	*gen import`i'=0
}
foreach i in $wiotcountry {
	gen Dummy_To_`i'=0
}
gen Trader = 0

foreach i in $wiotcountry {
		replace import`i'=0 if import`i'==.
		replace Dummy_From_`i'=Dummy_From_`i'+import`i'
		replace Dummy_From_`i'=1 if Dummy_From_`i'>0
		replace Dummy_From_`i'=0 if Dummy_From_`i'!=1

}

foreach i in $wiotcountry {
	replace export`i'=0 if export`i'==.
	replace Dummy_To_`i'=Dummy_To_`i'+export`i'
	replace Dummy_To_`i'=1 if Dummy_To_`i'>0
	replace Dummy_To_`i'=0 if Dummy_To_`i'!=1
}
foreach i in $wiotcountry {
	replace Trader = 1 if Dummy_From_`i'>0
	replace Trader = 1 if Dummy_To_`i'>0
} 
bys siren: egen trader = sum(Trader)
replace Trader = 1 if trader>0
drop trader

g catotal_=cafranc
g caexpor=0
foreach i in $wiotcountry {
	replace catotal_=catotal_+export`i' if export`i'!=.
	replace caexpor=caexpor+export`i' if export`i'!=.
}
foreach i in $wiotcountry {
	g export_intensity_`i'=0
	replace export_intensity_`i'=export`i'/catotal_ if export`i'!=.
}
g export_intensity = caexpor/catotal_
drop catotal_ cafranc


drop exportROW exportTWN Dummy_From_ROW importROW Dummy_From_TWN importTWN Dummy_To_ROW Dummy_To_TWN 
gegen ID=group(siren wiot12_jp)
foreach var in Real_GDPFRA Real_GDP_USD_oldFRA Real_GDP_USDFRA gr_Real_GDP_USDFRA gr_Real_GDP_USD_oldFRA{
	rename `var'  _`var'
} 
drop Real_GDPROW Real_GDP_USD_oldROW Real_GDP_USDROW gr_Real_GDPROW gr_Real_GDP_USDROW gr_Real_GDP_USD_oldROW

g Tot_export=0
g Tot_import=0
foreach i in $wiotcountry {
	capture replace Tot_export = Tot_export + export`i'
	capture replace Tot_import = Tot_import + import`i'
} 

foreach i in $wiotcountry {
	capture g export_share`i' = export`i' / Tot_export
	capture g import_share`i' = import`i' / Tot_import
	
}
drop gr_Real_GDP_USD_old* lag_Nominal_GDP_USDROW gr_Nominal_GDP_USDFRA  lag_Nominal_GDP_USDFRA
keep siren ID wiot12_jp year vabcf _gr_Real_GDPFRA gr_vabcf gr_vabcf_real gr_saltrai gr_saltrai_real gr_firm_inputs gr_firm_inputs_real gr_firm_import gr_firm_import_real Dummy_From_* Dummy_To_* gr_Real_GDP* gr_Real_GDP_USD* gr_Nominal_GDP_USD* lag_Nominal_GDP_USD* export_share* import_share* export_intensity* import_intensity* caexpor export* import* 
order siren ID wiot12_jp year vabcf gr_vabcf gr_vabcf_real gr_saltrai gr_saltrai_real gr_firm_inputs gr_firm_inputs_real gr_firm_import gr_firm_import_real Dummy_From_* Dummy_To_* gr_Real_GDP_USD* gr_Real_GDP*  gr_Nominal_GDP_USD* lag_Nominal_GDP_USD* _gr_Real_GDPFRA  export_share* import_share* export_intensity* import_intensity* caexpor export* import* 
compress
save "$outputpath/VA_GDP_Trade_wide_${sample}", replace
tab year if gr_vabcf_real!=.
gsort ID year
save "$outputpath/VA_GDP_Trade_wide_${sample}", replace
